package com.quicklife.action.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.quicklife.service.impl.RegisterServiceImpl;
import com.quicklife.util.FileFormHandler;

/**
 * 注册
 * SmartUpload组件控制文件上传
 * 
 * @author HackerD
 * 
 */
public class WebRegisterAction extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String rtinfos = "";
		FileFormHandler formHandler = new FileFormHandler(getServletConfig(), request, response);
		JSONObject rqinfos = formHandler.getParameterJson();
		
		String dir = getServletContext().getRealPath("/upload");
		String fileName = formHandler.singleFileUpload(dir);
		if (fileName == null) {
			rqinfos.put("logo", "/QuickLifeServer/images/default_business_logo.png");
		} else {
			rqinfos.put("logo", "/QuickLifeServer/upload" + File.separator + fileName);
		}

		// 执行验证
		RegisterServiceImpl rs = new RegisterServiceImpl();
		rtinfos = rs.webRegister(rqinfos.toString());
		
		// 注册成功或失败操作
		JSONObject registerResult = JSONObject.fromObject(rtinfos);
		if (registerResult.getString("result").equalsIgnoreCase("success")) {
			request.getSession().setAttribute("id", registerResult
					.getJSONObject("rtinfos").getString("id"));
			request.getSession().setAttribute("roleid", 1);
			request.getSession().setAttribute("name", rqinfos.getString("name"));
			request.getSession().setAttribute("logo", rqinfos.getString("logo"));
		} else {
			// 注册失败删掉图片
			File file = new File(dir + File.separator + fileName);
			if (file.isFile()) {
				file.delete();
			}
		}

		// 返回结果
		out.print(rtinfos);

		out.flush();
		out.close();
	}

}
